package com.sicheng.lc.周赛.分类.哈西表or计数or滑动窗口or双指针;

import leetcode.editor.cn.tree.TreeNode;

import java.util.Arrays;
import java.util.HashMap;

/**
 * @author zsc
 * @version 1.0
 * @date 2022/6/28 16:50
 */
public class 根据描述创建二叉树 {
    //https://leetcode.cn/problems/create-binary-tree-from-descriptions/

    public TreeNode createBinaryTree(int[][] descriptions) {
        HashMap<Integer, TreeNode> map = new HashMap<>();
        int[] st = new int[(int) (1e5 + 1)];
        Arrays.fill(st, -1);
        for (int[] description : descriptions) {
            TreeNode root = map.computeIfAbsent(description[0], k -> new TreeNode(description[0]));
            TreeNode kid = map.computeIfAbsent(description[1], k -> new TreeNode(description[1]));
            if (st[description[0]] == -1) {
                st[description[0]] = 0;
            }
            st[description[1]] = 1;
            if (description[2] == 1) {
                root.left = kid;
            } else {
                root.right = kid;
            }
        }
        for (int i = 1; i < st.length; i++) {
            if (st[i] == 0) {
                return map.get(i);
            }
        }

        return null;
    }
}
